/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module test.c - Fucntions which the tables |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Main/test.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>

#define VSTEST_C

#include <aclconio.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <acldev.h>
#include <aclstr.h>

#if defined (_VSC)
// #include <txoio.h>
#endif

#ifdef DEBUG_VSTEST
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <test.h>
#include <gds.h>
#include <print.h>
#include <pinpad.h>
#include <cdt.h>
#include <hdt.h>
#include <tct.h>
#include <trt.h>
#include <udt.h>
#include <msgclrk.h>
#include <lct.h>
#include <tblutil.h>
#include <msg.h>
#include <msgfuncs.h>
#include <ui.h>
#include <spdebug.h>

/*-------------------------------------------------------------------------
    Function:       inChkFileExist
    Description:    Check if File exists
    Parameters:     char *
    Returns:        VS_ERR, VS_SUCCESS      
    Globals :
    Notes :
--------------------------------------------------------------------------*/

int inChkFileExist(const char* szFileName) {
    FHANDLE hFHandle;

    hFHandle = open(szFileName, O_RDONLY);
    if (hFHandle < 0)
        return(VS_ERR);

    close(hFHandle);
    return(VS_SUCCESS);
}

int inTestTables(TRANSACTION_OBJECT* pobTran) {
    if (inTestAllTables() != VS_SUCCESS) {
        vdSGErrorMessage(TABLES_ERR_MSG);
        return (VS_ERR);
    }
    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function:       inTestAllTables
    Description:    Check if Tables exists
    Parameters:     
    Returns:        VS_ERR, VS_SUCCESS      
    Globals :
    Notes :
--------------------------------------------------------------------------*/

char* aszCoreTables[] = {
    "CPT",
    "FCT",
    "HDT",
    "CDT",
    "TCT",
    "UDT",
    "BDT",
    "CST",
    "DCT",
    "DPT",
    "\0"
};

int inTestAllTables() {
    int inFileNum = 0;
    char szDisplayBuffer[DISP_STR_LEN + 1];
    char szFileName[FILENAME_SIZE + 1];

    pdebug(("--inTestAllTables--"));

    vdDisplayMessageFromFile(TABLES_CHECK_MSG, STATUS_MSG);

    memset(szDisplayBuffer, 0, sizeof(szDisplayBuffer));

    while (aszCoreTables[inFileNum][0]) {
        strcpy(szFileName, aszCoreTables[inFileNum]);
        strcat(szFileName, DAT_SUFFIX);
        if (inChkFileExist(szFileName) != VS_SUCCESS) {
            strcpy(szDisplayBuffer, szFileName);
            break;
        }
        inFileNum++;
    }

    if (strlen(szDisplayBuffer) > 0) {
        char szBuf[6 + 1];   
        strcpy(szBuf, "-Error");
        strcat(szDisplayBuffer, szBuf);
        vdDisplayErrorString(szDisplayBuffer);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}

#undef VSTEST_C

